import {
  getQualityDetectionIndicatorList,
  addQualityDetectionIndicator,
  updateQualityDetectionIndicator,
  deleteQualityDetectionIndicator,
} from "@/api/basicData/inspectionIndicators.js";

import { getQualityDetectionProjectList } from "@/api/basicData/inspectionProject.js";

export const myMethod = {
  // 切换条数
  handleSizeChange(val) {
    this.pageSize = val;
    this.currentPage = 1;
    this.getTableData();
  },
  // 切换页码
  handleCurrentChange(val) {
    this.currentPage = val;
    this.getTableData();
  },
  // 点击查询操作
  queryChange(val) {
    this.form = Object.assign(this.form, val);
    this.currentPage = 1;
    this.getTableData();
  },
  // 重置查询数据
  resetPage() {
    this.pageSize = 15;
    this.currentPage = 1;
    this.totalPage = 0;
    this.$refs.dataTableRef.tableData = [];
    this.form = {};
  },
  // 获取列表数据
  async getTableData() {
    this.$refs.dataTableRef.tableLoading = true;
    try {
      const { qualityDetectionIndicatorName, WorkOrderCode } = this.form;
      const params = {
        QualityDetectionIndicatorId: '',
        QualityDetectionIndicatorCode: WorkOrderCode || "", // 质检指标编码
        QualityDetectionIndicatorName: qualityDetectionIndicatorName || "", // 质检指标名称
        rows: this.pageSize,
        page: this.currentPage,
      };
      console.log(params);
      const res = await getQualityDetectionIndicatorList(params);
      if (res.status === 0) {
        this.$refs.dataTableRef.tableData = res.rows || [];
        console.log(this.$refs.dataTableRef.tableData);
        this.totalPage = res.total || 0;
      } else {
        this.$refs.dataTableRef.tableData = [];
        this.totalPage = 0;
        this.$message.error(res.msg || "查询失败");
      }
      this.$refs.dataTableRef.tableLoading = false;
    } catch (err) {
      this.$message.error(err.msg || "查询失败");
      this.$refs.dataTableRef.tableLoading = false;
    }
  },
  addChange() {
    this.addTitle = "添加质检指标";
    this.$refs.addDialogComRef.dialogVisible = true;
    this.$nextTick(()=>{
      this.$refs.inspectionInfoRef.clearValidate();
    })
    this.getInspectionProjectList(true)
  },
  // 获取检验项目列表数据
  async getInspectionProjectList(type) {
    if(type) this.$refs.addDialogComRef.loading = true;
    try {
      const params = {
        QualityDetectionProjectCode: "",
        QualityDetectionProjectName: "",
        rows: 1000,
        page: 1,
      };
      const res = await getQualityDetectionProjectList(params);
      if (res.status === 0) {
        this.inspectionProjectData = res.rows || [];
        this.inspectionProjectDataTemp = res.rows || [];
      } else {
        this.$message.error(res.msg || "查询失败");
      }
      if(type) this.$refs.addDialogComRef.loading = false;
    } catch (err) {
      this.$message.error(err.msg || "查询失败");
      if(type) this.$refs.addDialogComRef.loading = false;
    }
  },
  visibleHandler() {
    this.inspectionProjectDataTemp = this.inspectionProjectData;
  },
  // 对数据进行模糊匹配
  querySearchAsync(queryString) {
    this.inspectionProjectDataTemp = this.inspectionProjectData.filter(
      (item) =>
        item.qualityDetectionProjectName.includes(queryString)
    );
  },
  handleSelect(id) {
    console.log('当前所选质检指标所属项目ID：', id);
  },
  // 清空弹框的数据
  closeChange() {
    this.inspectionInfo = {};
    this.$refs.inspectionInfoRef.clearValidate();
  },
  // 确认提交操作
  confirmChange() {
    this.$refs.inspectionInfoRef.validate(async (valid) => {
      if (valid) {
        try {
          this.$refs.addDialogComRef.loading = true;
          const {
            qualityDetectionIndicatorCode,
            qualityDetectionIndicatorName,
            qualityDetectionIndicatorDescription,
            qualityDetectionProjectId,
            remark,
          } = this.inspectionInfo;
          const params = {
            qualityDetectionIndicatorCode: qualityDetectionIndicatorCode, // 质检指标编码
            qualityDetectionIndicatorName: qualityDetectionIndicatorName, // 质检指标名称
            qualityDetectionIndicatorDescription: qualityDetectionIndicatorDescription, // 质检指标描述
            qualityDetectionProjectId: qualityDetectionProjectId, // 质检指标所属项目
            remark: remark || "", // 描述
          };
          const isAdd = !this.inspectionInfo.qualityDetectionIndicatorId;
          const text = isAdd ? "添加" : "修改";
          if (!isAdd) {
            params.qualityDetectionIndicatorId =
              this.inspectionInfo.qualityDetectionIndicatorId;
          }
          const apiName = isAdd
            ? addQualityDetectionIndicator
            : updateQualityDetectionIndicator;
          const result = await apiName(params);
          if (result.ok) {
            this.$message.success(text + "成功");
            this.$refs.addDialogComRef.dialogVisible = false;
            if(isAdd) this.currentPage = 1
            this.closeChange();
            this.getTableData();
          } else {
            this.$message.error((result.err&&result.err.msg) || (text + "失败"));
          }
          this.$refs.addDialogComRef.loading = false;
        } catch (err) {
          this.$refs.addDialogComRef.loading = false;
          console.error(text + "数据异常:", err);
        }
      }
    });
  },
  detailChange(val) {
    this.updateChange(val);
  },
  updateChange(val) {
    this.addTitle = "修改质检指标";
    this.inspectionInfo = JSON.parse(JSON.stringify(val))	;
    this.$refs.addDialogComRef.dialogVisible = true;
    this.$nextTick(()=>{
      this.$refs.inspectionInfoRef.clearValidate();
    })
    this.getInspectionProjectList()
  },
  async deleteChange(val) {
    // delNum用于切换页码
    const delNum = 1;
    const oldCurrentPage = this.currentPage;
    if (delNum) {
      const temp = Math.ceil(
        (this.totalPage - delNum ? this.totalPage - delNum : 1) / this.pageSize
      );
      if (this.currentPage > temp) {
        this.currentPage = temp;
      }
    }
    this.$refs.dataTableRef.tableLoading = true;
    try {
      const params = {
        id: val.qualityDetectionIndicatorId || "", // 质检指标id
      };
      const res = await deleteQualityDetectionIndicator(params);
      if (res.ok) {
        this.$message.success("删除成功");
        this.getTableData();
      } else {
        this.currentPage = oldCurrentPage;
        this.$message.error((res.err&&res.err.msg) || "删除失败");
      }
      this.$refs.dataTableRef.tableLoading = false;
    } catch (err) {
      this.currentPage = oldCurrentPage;
      this.$message.error((err.err&&err.err.msg) || "删除失败");
      this.$refs.dataTableRef.tableLoading = false;
    }
  },
};
